﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Details_Episode.xaml
    /// </summary>
    public partial class Details_Episode : UserControl
    {
        public int SelectedEpisodeID { get; set; }
        public Objects.SeriesEpisode SelectedEpisode  {get; set; }
        public Objects.SeriesSeason EpisodeSeason { get; set; }
        public Objects.Series EpisodeSeries { get; set; }

        Settings oSettings = new Settings();

        Controls.Library.Details_Season ParentSeasonDetails { get; set; }

        public Details_Episode(Controls.Library.Details_Season ctlParentSeasonDetails ,int iEpisodeID)
        {
            InitializeComponent();

            SelectedEpisodeID = iEpisodeID;
            SelectedEpisode = new Objects.SeriesEpisode(iEpisodeID);
            EpisodeSeason = new Objects.SeriesSeason(SelectedEpisode.SeasonID);
            EpisodeSeries = new Objects.Series(SelectedEpisode.SeriesID);
            ParentSeasonDetails = ctlParentSeasonDetails;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            /*********************************************************************
            * Get Base-Info
            * *******************************************************************/
            lblSeriesName.Content = EpisodeSeries.Name;

            // Add Season-Count.
            string sCounting = "S";
            if (EpisodeSeason.Number < 10)
                sCounting += "0";
            sCounting += EpisodeSeason.Number.ToString();

            // Add Episode-Count.
            sCounting += " E";
            if (SelectedEpisode.EpisodeNo < 10)
                sCounting += "0";
            sCounting += SelectedEpisode.EpisodeNo.ToString();

            // Add Title if any.
            if (SelectedEpisode.Name != String.Empty)
                sCounting += " - " + SelectedEpisode.Name;

            // Update GUI.
            lblEpisodeTitle.Content = sCounting;

            // Check for existing cover-art.
            /* Obsolete in new Layout!!5
            if (System.IO.File.Exists(oSettings.LibraryDataStorage + "series\\images\\xmm_" + EpisodeSeries.SeriesID.ToString() + ".jpg"))
            {
                BitmapImage bmpCoverArt = new BitmapImage(new Uri(oSettings.LibraryDataStorage + "series\\images\\xmm_" + EpisodeSeries.SeriesID.ToString() + ".jpg", UriKind.Absolute));
                imgBanner.Source = bmpCoverArt;
            }
             * */

            // Update Plot.
            txtPlot.Text = SelectedEpisode.Plot;

            /*********************************************************************
            * Get Audio-Info
            * *******************************************************************/
            if (spnAudioInfo != null && spnAudioInfo.Children.Count == 3)
            {
                spnAudioInfo.Children.RemoveAt(2);
                spnAudioInfo.Children.RemoveAt(1);
            }

            if (SelectedEpisode.AudioInfo != null)
            {
                StackPanel spnAudioQuality = new StackPanel();
                spnAudioQuality.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnAudioQuality.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                spnAudioQuality.Orientation = Orientation.Horizontal;

                Image imgMaxChannels = new Image();
                imgMaxChannels.Width = imgMaxChannels.Height = 24;
                imgMaxChannels.Stretch = Stretch.Uniform;
                imgMaxChannels.Source = new BitmapImage(new Uri(@"../../images/bin/channels_64.png", UriKind.Relative));
                spnAudioQuality.Children.Add(imgMaxChannels);

                // Get max channels.
                Label lblMaxChannels = new Label();
                lblMaxChannels.Content = SelectedEpisode.AudioInfo.Channels.ToString();
                spnAudioQuality.Children.Add(lblMaxChannels);

                // Get best audio quality for representation.
                Image imgAudioQuality = new Image();
                imgAudioQuality.Width = imgAudioQuality.Height = 24;
                imgAudioQuality.Stretch = Stretch.Uniform;

                if (SelectedEpisode.AudioInfo.IsDolbyDigital)
                {
                    imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dd.png", UriKind.Relative));                        
                    spnAudioQuality.Children.Add(imgAudioQuality);
                }
                else if (SelectedEpisode.AudioInfo.IsDolbySurround)
                {
                    imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dolby.png", UriKind.Relative));
                    spnAudioQuality.Children.Add(imgAudioQuality);
                }
                else if (SelectedEpisode.AudioInfo.IsDTS)
                {
                    imgAudioQuality.Source = new BitmapImage(new Uri(@"../../images/bin/dts.png", UriKind.Relative));
                    spnAudioQuality.Children.Add(imgAudioQuality);
                }

                // Get total audio tracks...
                StackPanel spnTracks = new StackPanel();
                spnTracks.Orientation = Orientation.Horizontal;

                Image imgAudioTracks = new Image();
                imgAudioTracks.Width = imgAudioTracks.Height = 24;
                imgAudioTracks.Stretch = Stretch.Uniform;
                imgAudioTracks.Source = new BitmapImage(new Uri(@"../../images/bin/tracks.png", UriKind.Relative));
                spnTracks.Children.Add(imgAudioTracks);

                Label lblAudioTracks = new Label();
                lblAudioTracks.Content = SelectedEpisode.AudioInfo.Tracks.ToString();
                spnTracks.Children.Add(lblAudioTracks);

                // add audio-tracks to gui.
                spnAudioQuality.Children.Add(spnTracks);

                // Add audio-quality info to gui.
                spnAudioInfo.Children.Add(spnAudioQuality);

                // Get audio-languages...
                StackPanel spnAudioLanguages = new StackPanel();
                spnAudioLanguages.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
                spnAudioLanguages.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnAudioLanguages.Orientation = Orientation.Horizontal;

                int iCurrentLanguage = 0;
                foreach (string sAudioLanguage in SelectedEpisode.AudioInfo.Languages)
                {
                    Image imgLanguage = new Image();
                    imgLanguage.Width = imgLanguage.Height = 24;
                    imgLanguage.Stretch = Stretch.Uniform;
                    string sImagePath = Tools.getInstallationPath() + "images\\lang\\" + Functions.LanguageTools.getNormalizedLanguage(sAudioLanguage) + ".png";
                    if (System.IO.File.Exists(sImagePath))
                    {
                        imgLanguage.Source = new BitmapImage(new Uri(@"../../images/lang/" + Functions.LanguageTools.getNormalizedLanguage(sAudioLanguage) + ".png", UriKind.Relative));
                        imgLanguage.ToolTip = sAudioLanguage + " (Track: " + iCurrentLanguage.ToString() + "; Codec: " + SelectedEpisode.AudioInfo.AudioCodecs[iCurrentLanguage] + ")";
                    }
                    else
                    {
                        imgLanguage.Source = new BitmapImage(new Uri(@"../../images/bin/unknown.png", UriKind.Relative));
                        imgLanguage.ToolTip = "Unbekannte Sprache \"" + sAudioLanguage + "\" (Track: " + iCurrentLanguage.ToString() + "; Codec: " + SelectedEpisode.AudioInfo.AudioCodecs[iCurrentLanguage] + ")";
                    }
                    spnAudioLanguages.Children.Add(imgLanguage);
                    ++iCurrentLanguage;
                }

                spnAudioInfo.Children.Add(spnAudioLanguages);
            }

            /*********************************************************************
                * Get Video-Info
                * *******************************************************************/
            if (spnVideoInfo != null && spnVideoInfo.Children.Count > 1)
            {
                for (int i = spnVideoInfo.Children.Count - 1; i > 0; i--)
                {
                    spnVideoInfo.Children.RemoveAt(i);
                }
            }

            if (SelectedEpisode.VideoInfo != null)
            {
                StackPanel spnVideoQuality = new StackPanel();
                spnVideoQuality.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                spnVideoQuality.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnVideoQuality.Orientation = Orientation.Horizontal;

                // Get Video-Tracks.
                Image imgVideoTracks = new Image();
                imgVideoTracks.Width = imgVideoTracks.Height = 24;
                imgVideoTracks.Source = new BitmapImage(new Uri(@"../../images/bin/tracks.png", UriKind.Relative));
                imgVideoTracks.ToolTip = SelectedEpisode.VideoInfo.Codec;
                spnVideoQuality.Children.Add(imgVideoTracks);

                Label lblVideoTracks = new Label();
                lblVideoTracks.Content = SelectedEpisode.VideoInfo.VideoTracks.ToString();
                lblVideoTracks.ToolTip = SelectedEpisode.VideoInfo.Codec;
                spnVideoQuality.Children.Add(lblVideoTracks);

                // Get Quality from HD-Level.
                Image imgVideoQuality = new Image();
                imgVideoQuality.Width = imgVideoQuality.Height = 32;
                imgVideoQuality.Stretch = Stretch.Uniform;

                // Assign HD-Level-Image.
                switch (SelectedEpisode.VideoInfo.HDLevel)
                {
                    case 0:
                        imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/sd.png", UriKind.Relative));
                        break;
                    case 1:
                        imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd720.png", UriKind.Relative));
                        break;
                    case 2:
                        imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd1080i.png", UriKind.Relative));
                        break;
                    case 3:
                        imgVideoQuality.Source = new BitmapImage(new Uri(@"../../images/bin/hd1080.png", UriKind.Relative));
                        break;
                }

                spnVideoQuality.Children.Add(imgVideoQuality);

                // Add quality-info to GUI.
                spnVideoInfo.Children.Add(spnVideoQuality);

                // Get Subtitle-Languages.
                StackPanel spnSubtitles = new StackPanel();
                spnSubtitles.Orientation = Orientation.Horizontal;
                spnSubtitles.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                spnSubtitles.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

                int iCurrentSubtitleTrack = 0;

                if (SelectedEpisode.VideoInfo.SubtitleTracks > 0)
                {
                    foreach (string sLanguage in SelectedEpisode.VideoInfo.SubtitleLanguages)
                    {
                        Image imgSubtitle = new Image();
                        imgSubtitle.Stretch = Stretch.Uniform;
                        imgSubtitle.Width = imgSubtitle.Height = 24;

                        string sLanguageImgPath = Tools.getInstallationPath() + "images\\lang\\" + Functions.LanguageTools.getNormalizedLanguage(sLanguage) + ".png";
                        if (System.IO.File.Exists(sLanguageImgPath))
                        {
                            imgSubtitle.Source = new BitmapImage(new Uri(@"../../images/lang/" + Functions.LanguageTools.getNormalizedLanguage(sLanguage) + ".png", UriKind.Relative));
                            imgSubtitle.ToolTip = sLanguage + " (Track: " + iCurrentSubtitleTrack.ToString() + ")";
                        }
                        else
                        {
                            imgSubtitle.Source = new BitmapImage(new Uri(@"../../images/bin/unknown.png", UriKind.Relative));
                            imgSubtitle.ToolTip = "Unbekannte Sprache \"" + sLanguage + "\" (Track: " + iCurrentSubtitleTrack.ToString() + ")";
                        }

                        spnSubtitles.Children.Add(imgSubtitle);
                        ++iCurrentSubtitleTrack;
                    }
                    spnVideoInfo.Children.Add(spnSubtitles);
                }

            }


            /*********************************************************************
                * Get File-Info
                * *******************************************************************/
            if (spnDiskMediaInfo != null && spnDiskMediaInfo.Children.Count > 1)
            {
                for (int i = spnDiskMediaInfo.Children.Count - 1; i > 0; i--)
                {
                    spnDiskMediaInfo.Children.RemoveAt(i);
                }
            }

            // Get DiskMedia-Info.
            StackPanel spnMediaInfo = new StackPanel();
            spnMediaInfo.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            spnMediaInfo.Orientation = Orientation.Horizontal;

            Image imgDiskMedia = new Image();
            imgDiskMedia.Stretch = Stretch.Uniform;
            imgDiskMedia.Width = imgDiskMedia.Height = 24;
            Label lblDiskMedia = new Label();

            if (SelectedEpisode.DiskMediaID != 0)
            {
                Objects.DiskMedia oMedia = new Objects.DiskMedia(SelectedEpisode.DiskMediaID);

                imgDiskMedia.Source = new BitmapImage(new Uri(@"../../images/bin/disk.png", UriKind.Relative));
                imgDiskMedia.ToolTip = oMedia.MediaName + " (Medium hinzugefügt: " + oMedia.MediaDate.ToShortDateString() + ")";
                lblDiskMedia.Content = oMedia.MediaName;
            }
            else
            {
                imgDiskMedia.Source = new BitmapImage(new Uri(@"../../images/bin/cdrom_64.png", UriKind.Relative));
                imgDiskMedia.ToolTip = lblDiskMedia.Content = "Separater Datenträger";
            }

            spnMediaInfo.Children.Add(imgDiskMedia);
            spnMediaInfo.Children.Add(lblDiskMedia);
            spnDiskMediaInfo.Children.Add(spnMediaInfo);

            // Get Files.
            StackPanel spnFileList = new StackPanel();
            spnFileList.Orientation = Orientation.Vertical;
            spnFileList.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            spnFileList.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;

            Label lblFileInfo = new Label();
            lblFileInfo.Content = "•  " + SelectedEpisode.MediaFile;
            if (System.IO.File.Exists(SelectedEpisode.MediaFile))
                lblFileInfo.Foreground = Brushes.DarkGreen;
            else
                lblFileInfo.Foreground = Brushes.Red;

            spnFileList.Children.Add(lblFileInfo);

            spnDiskMediaInfo.Children.Add(spnFileList);
        }

        private void btnPlayEpisode_Click(object sender, RoutedEventArgs e)
        {
            if(ParentSeasonDetails != null)
                ParentSeasonDetails.StartPlaybackThread(SelectedEpisode);
        }
    }
}
